/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.statistics;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.taglib.TagUtils;

import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>This tag inserts code that allows to count a user loading page containing
 * this tag.
 * </p>
 * <p><a href="CollectStatsTag.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.1 $ $Date: 2006/05/26 08:13:47 $
 * @jsp.tag name="collectStats"
 * body-content="empty"
 */
public class CollectStatsTag extends SimpleTagSupport {

    protected transient final Log log = LogFactory.getLog(CollectStatsTag.class);

    /**
     * Processes the tag.
     *
     * @throws javax.servlet.jsp.JspException
     */
	public void doTag() throws JspException {
        PageContext pageContext = (PageContext) getJspContext();
        TagUtils tagUtils = TagUtils.getInstance();
        String contextPath = ((HttpServletRequest) pageContext.getRequest()).getContextPath();
        if ("/".equals(contextPath)) {
            contextPath = "";
        }

        String baseUrl = contextPath + "/servlet/collectStats";
        String result = "<script language=\"JavaScript\"><!--\n"
                + "document.write('<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"" + baseUrl
                + "?l='+escape(window.location.href)+"
                + "'&r='+escape(document.referrer)+"
                + "'&rnd='+Math.random()"
                + "+((typeof(screen)=='undefined')?'':'&s='+screen.width+'x'+screen.height+'x'+(screen.colorDepth?"
                + "screen.colorDepth:screen.pixelDepth))+'\" />')//--></script>\n"
                + "<noscript><link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"" + baseUrl + "\" /></noscript>";

        tagUtils.write(pageContext, result);
    }
}
